//
// Created by xili on 2024/8/6 19:29.
// Go big or go home.
//
#include <vector>
#include <unordered_map>

using namespace std;

class Solution {
public:
    int countKDifference(vector<int> &nums, int k) {
        unordered_map<int, int> ump;
        int n = nums.size();
        int ans = 0;
        for (int i = 0; i < n; i++) {
            if (ump.count(nums[i] - k)) {
                ans += ump[nums[i] - k];
            }
            if (ump.count(nums[i] + k)) {
                ans += ump[nums[i] + k];
            }
            ump[nums[i]]++;
        }
        return ans;
    }
};